<?php
namespace app\api\controller;

class User extends Common {

    /**
     * 用户注册
     * @return  [json]  [注册返回结果]
     */
    public function register(){
        // 接收参数
        $data = $this->params;
        // 检查验证码
        $this->check_code($data['user_name'], $data['code']);
        $user_name_type = $this->check_username($data['user_name']);
        switch ($user_name_type) {
            case 'phone':
                $this->check_exist($data['user_name'], 'phone', 0);
                $data['user_phone'] = $data['user_name'];
                break;
            case 'email':
                $this->check_exist($data['user_name'], 'email', 0);
                $data['user_email'] = $data['user_name'];
                break;
        }
        // 将用户信息写入数据库
        unset($data['user_name']);
        // register time
        $data['user_rtime'] = time();
        $res = db('user')->insert($data);
        if (!$res) {
            $this->retrun_msg(0, '用户注册失败!');
        } else {
            $this->return_msg(1, '用户注册成功!');
        }
    }

    /**
     * 用户登录
     * @return  [json]  [注册返回结果]
     */
    public function login(){
        // 接收参数
        $data = $this->params;
        // 检测用户名
        $user_name_type = $this->check_username($data['user_name']);
        switch ($user_name_type) {
            case 'phone':
                $this->check_exist($data['user_name'], 'phone', 1);
                $db_res = db('user')
                    ->field('user_id,user_nickname,user_phone,user_email,user_rtime,user_pwd')
                    ->where('user_phone', $data['user_name'])
                    ->find();
                break;
            case 'email':
                $this->check_exist($data['user_name'], 'email', 1);
                $db_res = db('user')
                    ->field('user_id,user_nickname,user_phone,user_email,user_rtime,user_pwd')
                    ->where('user_email', $data['user_name'])
                    ->find();
                break;
        }
        if ($db_res['user_pwd'] !== $data['user_pwd']) {
            $this->return_msg(0, '用户名或者密码不正确!');
        } else {
            unset($db_res['user_pwd']); // 密码永不返回
            $this->return_msg(1, '登录成功!', $db_res);
        }
    }

    /**
     * 用户上传头像
     * @return  [json]  [注册返回结果]
     */
    public function upload_head_img() {
        // 接收参数
        $data = $this->params;
        // 上传文件,获得路径
        $head_img_path = $this->upload_file($data['user_icon'], 'head_img');
        // 存入数据库
        $res = db('user')->where('user_id', $data['user_id'])->setField('user_icon', $head_img_path);
        if ($res) {
            $this->return_msg(1, '头像上传成功!', $head_img_path);
        } else {
            $this->return_msg(0, '上传头像失败!');
        }
    }

    /**
     * 用户修改密码
     * @return  [json]  [注册返回结果]
     */
    public function change_pwd() {
        // 接收参数
        $data = $this->params;
        // 检查用户名并取出数据库中的密码
        $user_name_type = $this->check_username($data['user_name']);
        switch ($user_name_type) {
        case 'phone':
            $this->check_exist($data['user_name'], 'phone', 1);
            $where['user_phone'] = $data['user_name'];
            break;
        case 'email':
            $this->check_exist($data['user_name'], 'email', 1);
            $where['user_email'] = $data['user_name'];
            break;
        }
        // 判断原始密码是否正确
        $db_ini_pwd = db('user')->where($where)->value('user_pwd');
        if ($db_ini_pwd !== $data['user_ini_pwd']) {
            $this->return_msg(0, '原密码错误!');
        }
        // 把新的密码存入数据库
        $res = db('user')->where($where)->setField('user_pwd', $data['user_pwd']);
        if ($res !== false) {
            $this->return_msg(1, '密码修改成功!');
        } else {
            $this->return_msg(0, '密码修改失败!');
        }
    }

    /**
     * 用户找回密码
     * @return  [json]  [注册返回结果]
     */
    public function find_pwd() {
        $data = $this->params;
        $this->check_code($data['user_name'], $data['code']);
        $user_name_type = $this->check_username($data['user_name']);
        switch ($user_name_type) {
            case 'phone':
                $this->check_exist($data['user_name'], 'phone', 1);
                $where['user_phone'] = $data['user_name'];
                break;
            case 'email':
                $this->check_exist($data['user_name'], 'email', 1);
                $where['user_email'] = $data['user_name'];
                break;
        }
        // 修改数据库
        $res = db('user')->where($where)->setField('user_pwd', $data['user_pwd']);
        if ($res !== false) {
            $this->return_msg(1, '密码修改成功!');
        } else {
            $this->return_msg(0, '密码修改失败!');
        }
    }

    /**
     * 绑定手机号码 -- 停用
     * @return  [json]  [注册返回结果]
     */
    public function bind_phone() {
        $data = $this->params;
        // 检查验证码
        $this->check_code($data['phone'], $data['code']);
        // 修改数据库
        $res = db('user')->where('user_id', $data['user_id'])->setField('user_phone', $data['phone']);
        if ($res !== false) {
            $this->return_msg(1, '手机号绑定成功!');
        } else {
            $this->return_msg(0, '手机号绑定失败!');
        }
    }

    /**
     * 绑定邮箱号码 -- 停用
     * @return  [json]  [注册返回结果]
     */
    public function bind_email() {
        $data = $this->params;
        // 检查验证码
        $this->check_code($data['email'], $data['code']);
        // 修改数据库
        $res = db('user')->where('user_id', $data['user_id'])->setField('user_email', $data['email']);
        if ($res !== false) {
            $this->return_msg(1, '邮箱绑定成功!');
        } else {
            $this->return_msg(0, '邮箱绑定失败!');
        }
    }

    /**
     * 合成上面两个（手机和邮箱）接口
     * @return  [json]  [注册返回结果]
     */
    public function bind_username() {
        $data = $this->params;
        // 检测验证码
        $this->check_code($data['user_name'], $data['code']);
        // 判断用户名
        $user_name_type = $this->check_username($data['user_name']);
        switch ($user_name_type) {
        case 'phone':
            $type_text                 = '手机号';
            $update_data['user_phone'] = $data['user_name'];
            break;
        case 'email':
            $type_text                 = '邮箱';
            $update_data['user_email'] = $data['user_name'];
            break;
        }
        // 修改数据库
        $res = db('user')->where('user_id', $data['user_id'])->update($update_data);
        if ($res !== false) {
            $this->return_msg(1, $type_text . '绑定成功!');
        } else {
            $this->return_msg(0, $type_text . '绑定失败!');
        }
    }

    /**
     * 设置用户昵称
     * @return  [json]  [注册返回结果]
     */
    public function set_nickname() {
        $data = $this->params;
        // 检测昵称
        $res = db('user')->where('user_nickname',$data['user_nickname'])->find();
        if ($res) {
            $this->return_msg(0,'该昵称已被占用!');
        }
        // 写入数据库
        $res = db('user')->where('user_id',$data['user_id'])->setField('user_nickname',$data['user_nickname']);
        if (!$res) {
            $this->return_msg(0,'修改昵称失败!');
        }else{
            $this->return_msg(1,'昵称修改成功!');
        }
    }

}